package com.pzhuweb.sgblog.service.impl;

import com.pzhuweb.sgblog.domain.entity.LoginUser;
import com.pzhuweb.sgblog.domain.entity.User;
import com.pzhuweb.sgblog.domain.utils.*;
import com.pzhuweb.sgblog.service.AdminLoginService;
import com.pzhuweb.sgblog.service.MenuService;
import com.pzhuweb.sgblog.service.RoleService;
//import com.pzhuweb.sgblog.utils.*;
import com.pzhuweb.sgblog.domain.vo.AdminInfoVo;
import com.pzhuweb.sgblog.domain.vo.UserInfoVo;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 鄧浩奇
 * @classname AdminLoginServiceImpl
 * @description
 * @since 2023/8/6 16:54
 **/

@Service
public class AdminLoginServiceImpl implements AdminLoginService {
    @Resource
    private AuthenticationManager authenticationManager;

    @Resource
    private RedisCache redisCache;

    @Resource
    private MenuService menuService;

    @Resource
    private RoleService roleService;

    @Override
    public ResponseResult<?> login(User user) {
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user.getUserName(), user.getPassword());
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        //判断是否认证通过
        if (Objects.isNull(authenticate)) {
            throw new RuntimeException("用户名或密码错误");
        }
        //获取userid 生成token
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
        String userId = loginUser.getUser().getId().toString();
        String jwt = JwtUtil.createJWT(userId);
        //把用户信息存入redis
        redisCache.setCacheObject("adminLogin:" + userId, loginUser);

        //把token封装 返回
        Map<String, String> map = new HashMap<>();
        map.put("token", jwt);
        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult<?> getInfo() {
        // 获取当前登陆的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 根据id查询权限信息
        assert loginUser != null;
        List<String> permsList = menuService.getPermsByUserId(loginUser.getUser().getId());
        // 根据id查询角色信息
        List<String> roleKeyList = roleService.getRolesKeyByUserId(loginUser.getUser().getId());
        // 封装vo返回
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(loginUser.getUser(), UserInfoVo.class);
        AdminInfoVo adminInfoVo = new AdminInfoVo(permsList,roleKeyList,userInfoVo);
        return ResponseResult.okResult(adminInfoVo);
    }

    @Override
    public ResponseResult<?> logout() {
        //获取当前登录的用户id
        Long userId = SecurityUtils.getUserID();
        //删除redis中对应的值
        redisCache.deleteObject("adminLogin:"+userId);
        return ResponseResult.okResult();
    }
}

